<template>
  <div class="mod-attr">
    <el-form
      size="small"
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.name"
          placeholder="属性名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.type" placeholder="属性类型" clearable>
          <el-option
            v-for="item in typeArrays"
            :label="item.name"
            :value="item.value"
            :key="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>

    图排还没做
    <el-table
      :data="dataList"
      border
      size="mini"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      ></el-table-column>
      <el-table-column type="index" align="center" label="编号" width="80" />
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="属性名"
      ></el-table-column>

      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="属性规格"
      >
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.type == 1">规格参数</el-tag>
          <el-tag type="success" v-else>销售参数</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="searchState"
        header-align="center"
        align="center"
        label="是否检索"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.searchState">是</el-tag>
          <el-tag type="info" v-else>否</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="showState"
        header-align="center"
        align="center"
        label="显示状态"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.showState">是</el-tag>
          <el-tag type="info" v-else>否</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="multipleState"
        header-align="center"
        align="center"
        label="属性类型"
      >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.multipleState">多选</el-tag>
          <el-tag v-else>单选</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="icon"
        header-align="center"
        align="center"
        label="图标"
      ></el-table-column>
      <el-table-column
        prop="valueList"
        header-align="center"
        align="center"
        label="值列表"
      >
        <template slot-scope="scope">
          <el-tooltip placement="top" v-if="scope.row.valueList">
            <div slot="content">
              <span
                v-for="(i, index) in scope.row.valueList.split(';')"
                :key="index"
              >
                {{ i }}
                <br />
              </span>
            </div>
            <el-tag>
              {{ scope.row.valueList.split(";")[0] + " ..." }}
            </el-tag>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        prop="dataState"
        header-align="center"
        align="center"
        label="启用状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dataState === -1" size="small" type="danger"
            >禁用</el-tag
          >
          <el-tag v-else size="small">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">

         <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row, 2)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >

          <el-button
            type="text"
            size="small"
            v-if="scope.row.dataState === -1"
            @click="enableHandle(scope.row)"
            >启用</el-button
          >

          <el-button
            type="text"
            size="small"
            v-else
            @click="disableHandle(scope.row)"
            >冻结</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

  </div>
</template>

<script>
import AddOrUpdate from "./attr-add-or-update";
import {
  productAttrPageList,
  removeProductAttr,
  enableProductAttr,
  disableProductAttr,
} from "@/api/product";
export default {
  components: { AddOrUpdate },
  data() {
    return {
      typeArrays: [
        { value: 1, name: "规格属性" },
        { value: 0, name: "销售属性" },
      ],
      dataForm: {
        name: "",
        type: "",
        catId: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      productAttrPageList({
        type: this.dataForm.type,
        categoryId: this.dataForm.catId,
        name: this.dataForm.name,
        pageNumber: this.pageIndex,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res && res.code === 0) {
          this.dataList = res.data.data;
          this.totalPage = res.data.count;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(data) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data);
      });
    },
    enableHandle(row) {
      this.$confirm(`确定进行启用操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          enableProductAttr({ id: row.id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    disableHandle(row) {
      this.$confirm(`确定进行冻结操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          disableProductAttr({ id: row.id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(`确定是否进行[${id ? "删除" : "批量删除"}]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        removeProductAttr({ ids: ids }).then((res) => {
          if (res && res.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              },
            });
          } else {
            this.$message.error(res.msg);
          }
        });
      });
    },
  },
};
</script>
<style scoped>
.t {
  clear: both;
  margin-bottom: 20px;
}
</style>